<?php
$shippingId = $this->getRequest()->getParam('id');
/** @var Silk_Tms_Model_Shipping $shipping */
$shipping = Mage::getModel('tms/shipping')->load($shippingId);
$db = Mage::getSingleton('core/resource')->getConnection('core_read');
$sql = "select * from tms_shipping_order WHERE shipping_id = {$shippingId}";
$query = $db->query($sql);
$shippingCollection = $query->fetchAll();

/*get caozuo ren*/
$sql = "select * from tms_shipping WHERE id = {$shippingId}";
$query = $db->query($sql);
$shippingInfoCollection = $query->fetchAll();

if ($shippingInfoCollection) {
    $adminId = $shippingInfoCollection[0]['admin_id'];
    $userId = $shippingInfoCollection[0]['user_id'];
    $shippingStatus = $shippingInfoCollection[0]['status'];

    $admin = Mage::getModel('admin/user')->load($adminId);
    $user = Mage::getModel('admin/user')->load($userId);
}

/** @var Silk_Tms_Model_Mysql4_Shipping_Order_Collection $shippingOrders */
$shippingOrders = Mage::getModel('tms/shipping_order')->getCollection();
$shippingOrders->addFieldToFilter('shipping_id', $shippingId);

$canCancelBind = false;
foreach ($shippingOrders as $shippingOrder) {
    if (!in_array($shippingOrder->getStatus(), array(
        Silk_Tms_Helper_Status::STATUS_ORDER_NEW,
        Silk_Tms_Helper_Status::STATUS_ORDER_PROCESSING
    ))
    ) {
        $canCancelBind = true;
        break;
    }
}

$startshipping = $this->getUrl('admin_tms/adminhtml_shipping/startshipping', array('id' => $shippingId));
$pickUrl = $this->getUrl('admin_tms/adminhtml_shipping/pickupAll', array('id' => $shippingId));
$lockUrl = $this->getUrl('admin_tms/adminhtml_shipping/lock', array('shipping_ids' => $shippingId));
$unlockUrl = $this->getUrl('admin_tms/adminhtml_shipping/unlock', array('shipping_ids' => $shippingId));
$pickupSuccess = $this->getUrl('admin_tms/adminhtml_shipping/pickupSuccess', array('id' => $shippingId));
$itemsTotal = 0;
$itemsTotalAll = 0;
$amountTotal = 0;
$isLock = $shipping->isLock();


?>
<div class="shipping-status" style="display:none"><?php echo $shipping->getStatus(); ?></div>
<div class="content-header">
    <h3 class="icon-head head-sales-order-invoice">运单详情 #<?php echo $shipping->getIncrementId(); ?></h3>
    <h3 class="icon-head"><?php echo $isLock ? '已锁定' : ''; ?></h3>
    <p class="form-buttons">
        <button id="id_7ecf0ab045f01f0c1081d8dc04dc0e57" title="返回" type="button" class="scalable back"
                onclick="javascript:history.back()" style="">
            <span><span><span>返回</span></span></span>
        </button>
        <?php if (Mage::getSingleton('admin/session')->isAllowed('sales/shipping/actions/lock') && !$isLock): ?>
            <button id="id_218290db0302b563099be2b23019b804" title="锁定运单" type="button" class="scalable go submit-button"
                    onclick="setLocation('<?php echo $lockUrl; ?>')" style="">
                <span><span><span>锁定运单</span></span></span>
            </button>
        <?php endif; ?>
        <?php if (Mage::getSingleton('admin/session')->isAllowed('sales/shipping/actions/unlock')): ?>
            <button id="id_218290db0302b563099be2b23019b804" title="解锁运单" type="button" class="scalable go submit-button"
                    onclick="setLocation('<?php echo $unlockUrl; ?>')" style="">
                <span><span><span>解锁运单</span></span></span>
            </button>
        <?php endif; ?>
        <?php if (Mage::getSingleton('admin/session')->isAllowed('sales/shipping/actions/pickupsuccess')): ?>
            <button id="jianhuo" title="拣货完成" type="button" class="scalable go submit-button"
                    onclick="setLocation('<?php echo $pickupSuccess; ?>')" style="">
                <span><span><span>拣货完成</span></span></span>
            </button>
        <?php endif; ?>
        <?php if (in_array($shipping->getStatus(), array(Silk_Tms_Helper_Status::STATUS_SHIPPING_ASSIGNED, Silk_Tms_Helper_Status::STATUS_SHIPPING_SORTED))): ?>
            <?php if (Mage::getSingleton('admin/session')->isAllowed('sales/shipping/actions/pickupall')): ?>
            <button id="quhuo" title="取货" type="button" class="scalable go submit-button"
                    onclick="setLocation('<?php echo $pickUrl; ?>')" style="">
                <span><span><span>全部取货</span></span></span>
            </button>
            <?php endif; ?>
        <?php endif; ?>
        
        <?php if (Mage::getSingleton('admin/session')->isAllowed('sales/shipping/actions/startshipping')): ?>
        <?php if ($shipping->getStatus()=='sorted'): ?>
            <button id="startshipping" title="开始配送" type="button" class="scalable go submit-button"
                    onclick="setLocation('<?php echo $startshipping; ?>')" style="">
                <span><span><span>开始配送</span></span></span>
            </button>
        <?php endif; ?>
        <?php endif; ?>
        
        <?php if (in_array($shipping->getStatus(), array(Silk_Tms_Helper_Status::STATUS_SHIPPING_ASSIGNED, Silk_Tms_Helper_Status::STATUS_SHIPPING_SORTED, Silk_Tms_Helper_Status::STATUS_SHIPPING_NEW))): ?>
            <?php if (!$isLock): ?>
                <?php if (Mage::getSingleton('admin/session')->isAllowed('sales/shipping/actions/deleteshipping')): ?>
                <button id="id_23868b56689ba3bbaca362b19f9dfb29" title="删除选择" type="button"
                        class="scalable save submit-button"
                        onclick="disableElements('submit-button');$('shipping_form').submit()" style="">
                    <span><span><span>删除选择</span></span></span>
                </button>
                <?php endif; ?>
            <?php endif; ?>
            <?php if (!$isLock): ?>
                <?php if (Mage::getSingleton('admin/session')->isAllowed('sales/shipping/actions/saveshipping')): ?>
                <button title="保存修改" type="button" onclick="disableElements('submit-button');change();"
                        class="scalable save submit-button">
                    <span><span><span>保存修改</span></span></span>
                </button>
                <?php endif; ?>
            <?php endif; ?>
        <?php endif; ?>
    </p>
</div>

<div>
    <div class="entry-edit">
        <div class="entry-edit-head">
            <h4 class="icon-head head-products">订单列表</h4>
        </div>
    </div>

    <form action="<?php echo $this->getUrl('tms/adminhtml_shipping/deletechecked'); ?>" method="post"
          name="shipping_form" id="shipping_form">
        <div class="grid np">
            <div class="hor-scroll">
                <?php echo $this->getBlockHtml('formkey') ?>
                <table cellspacing="0" class="data order-tables">
                    <colgroup>
                        <col width="1">
                        <col width="1">
                        <col width="1">
                        <col width="1">
                        <col>
                    </colgroup>
                    <thead>
                    <tr class="headings">
                        <th>
                            <input type="hidden" name="shipping_id" value="<?php echo $shippingId ?>"/>
                            <input type="checkbox" title="全选" id="checkout-all"/><span>全选</span>
                        </th>
                        <th>订单号</th>
                        <th style="width: 6%;"><span class="nobr">收货人</span></th>
                        <th style="width: 12%;"><span class="nobr">店铺名称</span></th>
                        <th><span class="nobr">收货人电话</span></th>
                        <th><span class="nobr">订单状态</span></th>
                        <th><span class="nobr">领取时间</span></th>
                        <th><span class="nobr">下单时间</span></th>
                        <th><span class="nobr">商品数</span></th>
                        <th><span class="nobr">商品总数</span></th>
                        <th><span class="nobr">订单总额</span></th>
                        <th><span class="nobr">折扣额</span></th>
                        <th><span class="nobr">实际付款</span></th>
                        <th><span class="nobr">收货地址</span></th>
                        <th><span class="nobr">订单排序</span></th>
                      <!--   <?php if (in_array($shipping->getStatus(), array(Silk_Tms_Helper_Status::STATUS_SHIPPING_ASSIGNED, Silk_Tms_Helper_Status::STATUS_SHIPPING_SORTED, Silk_Tms_Helper_Status::STATUS_SHIPPING_NEW)) && !$isLock): ?>
                            <th class="last"><span class="nobr">操作</span></th>
                        <?php endif; ?> -->
                    </tr>
                    </thead>
                    <tbody class="even">
                    <?php foreach ($shippingCollection as $order): ?>
                        <tr class="border">
                            <td>
                                <input type="checkbox" name="order_id[]" value="<?php echo $order['order_id']; ?>"
                                       class="massaction-checkbox">
                            </td>
                            <td><?php echo $order['increment_id']; ?></td>
                            <td><?php echo $order['customer_name']; ?></td>
                            <td><?php echo $order['customer_store_name']; ?></td>
                            <td><?php echo $order['customer_phone']; ?></td>
                            
                            <td>
                                <?php
                                echo Mage::helper('tms/status')->getStatus($order['status']);
                                ?>
                            </td>
                            <td style="display:none;" class="order-status"><?php echo $order['status']; ?></td>
                            <td><?php echo $order['pickup_at']; ?></td>
                            <?php
                            //add by wanglin start
                            $sql_1 = "select * from `sales_flat_order` WHERE entity_id = {$order['order_id']}";
                            $query_1 = $db->query($sql_1);
                            $time_result = $query_1->fetchAll();
                            $timestamp = strtotime($time_result[0]['created_at']) + 3600*8;
                            $createdAt = date("Y-m-d H:i:s", $timestamp);
                            //add by wanglin end
                            ?>
                            <td><?php echo $createdAt; ?></td>
                            <?php
                            /** @var Mage_Sales_Model_Order $mageOrder */
                            $mageOrder = Mage::getModel('sales/order')->load($order['order_id']);
                            $itemsCount = $mageOrder->getTotalItemCount();

                            $itemsTotal += $itemsCount;

                            $orderedQty = $mageOrder->getTotalQtyOrdered();

                            $itemsTotalAll += $orderedQty;
                            $amountTotal += $order['grand_total'];
                            ?>
                            <td><?php echo $itemsCount; ?></td>
                            <td><?php echo $orderedQty; ?></td>
                            <td><?php echo $order['grand_total']; ?></td>
                            <td><?php echo $order['discount_amount']; ?></td>
                            <td><?php echo $order['total_paid']; ?></td>
                            <td><?php echo $order['customer_address']; ?></td>
                            <td>
                                <input type="text" name="sort[<?php echo $order['order_id'] ?>]"
                                       value="<?php echo $order['sort'] ?>" title="订单排序" style="width: 50px;"/>
                            </td>
                            <!-- <?php if (in_array($shipping->getStatus(), array(Silk_Tms_Helper_Status::STATUS_SHIPPING_ASSIGNED, Silk_Tms_Helper_Status::STATUS_SHIPPING_SORTED, Silk_Tms_Helper_Status::STATUS_SHIPPING_NEW)) && !$isLock): ?>
                                <td>
                                    <a href="<?php echo Mage::getSingleton("adminhtml/url")->getUrl('*/*/delete', array('id' => $order['order_id'], 'shippingid' => $shippingId)) ?>">删除</a>
                                </td>
                            <?php endif; ?> -->
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <div class="clear"></div>

            <?php
            $websiteId = $shipping->getWebsiteId();
            $region = Mage::helper("tms")->getRegion($websiteId);
            $car = Mage::helper("tms")->getCar($websiteId);
            $regionId = $shipping->getRegionId();
            $carId = $shipping->getCarId();
            ?>
            <div class="box-left" style="margin-top: 20px">
                <div class="entry-edit">
                    <div class="entry-edit-head">
                        <h4 class="icon-head head-products">分拣区域</h4>
                    </div>
                    <fieldset>
                            <span class="field-row">
                                <?php if ($shipping->getStatus() == Silk_Tms_Helper_Status::STATUS_SHIPPING_DELIVERING || $shipping->getStatus() == Silk_Tms_Helper_Status::STATUS_SHIPPING_DELIVERED): ?>
                                    <label class="normal" for="history_comment">分拣区域</label>
                                    <?php echo $shipping->getRegion(); ?>
                                <?php else: ?>
                                    <label class="normal" for="history_comment">分拣区域</label>
                                    <select id="region" name="region_id" class="required-entry select">
                                <option></option>
                                        <?php foreach ($region as $k => $value): ?>
                                            <?php if ($k == $regionId): ?>
                                                <option value="<?php echo $k; ?>"
                                                        selected="selected"><?php echo $value; ?></option>
                                            <?php else: ?>
                                                <option value="<?php echo $k; ?>"><?php echo $value; ?></option>
                                            <?php endif; ?>
                                        <?php endforeach; ?>
                                </select>
                                <?php endif; ?>
                            </span>
                    </fieldset>
                    <div class="entry-edit-head">
                        <h4 class="icon-head head-products">车载信息</h4>
                    </div>
                    <fieldset>
                             <span class="field-row">
                                <?php if ($shipping->getStatus() == Silk_Tms_Helper_Status::STATUS_SHIPPING_DELIVERING || $shipping->getStatus() == Silk_Tms_Helper_Status::STATUS_SHIPPING_DELIVERED): ?>
                                    <label class="normal" for="history_comment">车载编号</label>
                                    <?php echo $shipping->getCar(); ?>
                                <?php else: ?>
                                    <label class="normal" for="history_comment">车载编号</label>
                                    <select id="car" name="car_id" class="required-entry select">
                                <option></option>
                                        <?php foreach ($car as $k => $value): ?>
                                            <?php if ($k == $carId): ?>
                                                <option value="<?php echo $k; ?>"
                                                        selected="selected"><?php echo $value; ?></option>
                                            <?php else: ?>
                                                <option value="<?php echo $k; ?>"><?php echo $value; ?></option>
                                            <?php endif; ?>
                                        <?php endforeach; ?>
                                </select>
                                <?php endif; ?>
                    </fieldset>
                </div>
            </div>

            <div class="box-right" style="margin-top: 20px">
                <div class="entry-edit">
                    <table>
                        <tr>
                            <td>商品数总计</td>
                            <td><?php echo $itemsTotal ?></td>
                        </tr>
                        <tr>
                            <td>商品总数总计</td>
                            <td><?php echo $itemsTotalAll ?></td>
                        </tr>
                        <tr>
                            <td>订单总额总计</td>
                            <td>￥ <?php echo $amountTotal ?></td>
                        </tr>
                        <tr>
                            <td>操作人</td>
                            <td><?php echo $admin->getFirstname() ?></td>
                        </tr>
                        <tr>
                            <td>配送员</td>
                            <td><?php
                                if ($user->getUsername() !== '') {
                                    echo $user->getFirstname();
                                } else {
                                    echo '未分配配送人员';
                                }
                                ?></td>
                        </tr>
                        <tr>
                            <td>备注</td>
                            <td><?php echo $shippingInfoCollection[0]['comment'] ?></td>
                        </tr>
                    </table>
                    <?php if (in_array($shipping->getStatus(), array(Silk_Tms_Helper_Status::STATUS_SHIPPING_ASSIGNED, Silk_Tms_Helper_Status::STATUS_SHIPPING_NEW)) && !$isLock): ?>
                        <div class="order-totals" style="margin-top: 20px;">
                            <div class="order-totals-bottom">
                                <div class="a-right">
                                    <?php if ($shipping->getStatus() == Silk_Tms_Helper_Status::STATUS_SHIPPING_ASSIGNED && !$canCancelBind): ?>
                                        <?php if (Mage::getSingleton('admin/session')->isAllowed('sales/shipping/actions/cancelshipping')): ?>
                                        <button id="id_23868b56689ba3bbaca362b19f9dfb21" title="取消分配" type="button"
                                                class="scalable save submit-button"
                                                onclick="disableElements('submit-button');$('shipping_form_cancel').submit()"
                                                style=""><span>取消分配</span></button>
                                        <?php endif; ?>
                                    <?php endif; ?>
                                    <?php if (Mage::getSingleton('admin/session')->isAllowed('sales/shipping/actions/assignshipping')): ?>
                                    <button id="id_23868b56689ba3bbaca362b19f9dfb29" title="运单分配" type="button"
                                            class="scalable save submit-button"
                                            onclick="disableElements('submit-button');$('shipping_form_assign').submit()"
                                            style=""><span><span><span>
                                    <?php
                                    if ($user->getUsername()) {
                                        echo '运单再分配';
                                    } else {
                                        echo '运单分配';
                                    }
                                    ?>
                                    </span></span></span></button>
                                <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </form>
    <form action="<?php echo $this->getUrl('tms/adminhtml_shipping/cancelShippingUser'); ?>" method="post"
          name="shipping_form" id="shipping_form_cancel">
        <?php echo $this->getBlockHtml('formkey') ?>
        <input type="hidden" name="shipping_id" value="<?php echo $shippingId ?>"/>
    </form>
    <form action="<?php echo $this->getUrl('tms/adminhtml_shipping/showbinduser'); ?>" method="post"
          name="shipping_form" id="shipping_form_assign">
        <?php echo $this->getBlockHtml('formkey') ?>
        <input type="hidden" name="ids" value="<?php echo $shippingId ?>"/>
    </form>
</div>

<script type="text/javascript">
    (function ($) {
        if (<?php echo count($shippingCollection) ?> == 0
        )
        {
            $('#id_23868b56689ba3bbaca362b19f9dfb29').attr("disabled", true);
        }

        $('#checkout-all').bind('change', function () {
            $('.massaction-checkbox').prop('checked', this.checked);
        });
        var shipingstatus = $(".shipping-status").html();
        console.log(shipingstatus);
        var orderstatus = $("td.order-status:last").html();
        console.log(orderstatus);
        if(orderstatus=="processing" && shipingstatus=="assigned"){
            $("#quhuo").css("display","none");
        }
        if(orderstatus=="processing" && shipingstatus=="sorted"){
            $("#jianhuo").css("display","none");
            $("#startshipping").css("display","none");
            
        }
        if(orderstatus=="picked" && shipingstatus=="sorted"){
            $("#jianhuo").css("display","none");
            $("#quhuo").css("display","none");
            
        }

        if(shipingstatus=="delivering"){
            $("#jianhuo").css("display","none");
        }
        if(shipingstatus=="delivered"){
            $("#jianhuo").css("display","none");
        }
    })(jQuery);

    function change() {
        var form = jQuery('#shipping_form');
        form.prop('action', "<?php echo $this->getUrl('tms/adminhtml_shipping/change'); ?>");
        form.submit();
    }
</script>

<style type="text/css">
    .add-shipping-to-oldshipping {
        background: #ea7601;
        color: #fff;
        border: 1px solid #ccc;
        height: 20px;
        display: inline-block;
        text-decoration: none;
        width: 72px;
        text-align: center;
    }

    a.add-shipping-to-oldshipping:hover {
        background: #D88634;
        color: #fff;
    }
</style>